# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=newton-dynamics
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.14c
pkgrel=2
pkgdesc='Newton dynamics is a realistic, cross-platform physics simulation library. '
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-tinyxml")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('zlib')
url='http://newtondynamics.com/'
source=("https://github.com/MADEAPPS/newton-dynamics/archive/v${pkgver}.tar.gz"
        "0001-fix-arch-detection.patch"
        "0002-use-msys-tinyxml.patch"
        "0003-fix-newton-h.patch")
sha256sums=('042342e021a429f4b689bf7aa2ed5b6d4b9b7abcde0eea57daa5873736073d22'
            '992696a1dc4dc7f054f9f8e5a2fd73a30c68d865efad96ba9b428d135a2c6bb3'
            '034a2aee4d1d3e5ca185ca8e86e91244a50cbbe07cf83d1ca1eb12d1ed100ae5'
            'dc81ddb8dfbca2c0979b90802365843211d59a1772c2bf8881c813fb5a05cb4a')
		 
prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-arch-detection.patch
  patch -p1 -i "${srcdir}"/0002-use-msys-tinyxml.patch
  # https://github.com/MADEAPPS/newton-dynamics/commit/a6782a4c50c68964d20ca8ebf9621da14bf8ebf0
  patch -p1 -i "${srcdir}"/0003-fix-newton-h.patch
}

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -G"MSYS Makefiles" \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_BUILD_SHARED_LIBS=OFF \
    -DNEWTON_WITH_AVX_PLUGIN=OFF \
    -DNEWTON_WITH_REFERENCE_GPU_PLUGIN=OFF \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}
	
    ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -G"MSYS Makefiles" \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_WITH_SSE4_PLUGIN=OFF \
    -DNEWTON_WITH_AVX2_PLUGIN=OFF \
    -DNEWTON_WITH_AVX_PLUGIN=ON \
    -DNEWTON_WITH_REFERENCE_GPU_PLUGIN=ON \
    -DNEWTON_BUILD_SHARED_LIBS=ON \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}
	
    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install
  
  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
}
